package com.kchouse;


import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.kchouse.common.BaseFragment;
import com.kchouse.mine.CollectionBusinessesActivity;
import com.kchouse.mine.CollectionGoodsActivity;
import com.kchouse.mine.LoginPhoneActivity;
import com.kchouse.mine.MineAddressActivity;
import com.kchouse.mine.MineCarActivity;
import com.kchouse.mine.MineCouponsActivity;
import com.kchouse.mine.MineprestoreActivity;
import com.kchouse.mine.MineMessageActivity;
import com.kchouse.mine.MineSetActivity;
import com.kchouse.mine.OrderActivity;
import com.kchouse.mine.RecommendActivity;


/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment implements View.OnClickListener {


    private View view;
    private Intent intent = new Intent();
    private FragmentActivity mContent;
    private boolean isLogin = false;
    private GridView gv_item;
    private LayoutInflater inflater;
    //图标
    private int icon[] = {R.mipmap.ic_car, R.mipmap.ic_coupons, R.mipmap.ic_mes4,
            R.mipmap.ic_message_blue, R.mipmap.ic_merchants, R.mipmap.ic_product, R.mipmap.ic_addr,
            R.mipmap.ic_set, R.mipmap.ic_service};
    //图标下的文字
    private String name[] = {"我的车辆", "我的卡券", "我的预存", "我的消息", "我的关注", "我的收藏", "我的地址", "设置", "客服"};
    private TextView tv_mobile;
    private StringBuffer sb = new StringBuffer();

    public MineFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        if (view == null) {
            this.inflater = inflater;
            view = inflater.inflate(R.layout.fragment_mine, null);
            intiData();
            intiView();

        }
        return view;
    }


    private void intiData() {
        mContent = getActivity();


    }

    private void intiView() {

        tv_mobile = (TextView) view.findViewById(R.id.tv_mobile);
        gv_item = (GridView) view.findViewById(R.id.gv_item);
        gv_item.setAdapter(new gvAdapter());
        gv_item.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


                // 如果未登录都进入登录界面
                if(!isLogin)
                {
                    intent.setClass(mContent, LoginPhoneActivity.class);
                    startActivity(intent);
                    return;
                }

                switch (position) {
                    case 0://我的车辆
                        intent.setClass(mContent, MineCarActivity.class);
                        startActivity(intent);
                        break;
                    case 1://优惠券
                        intent.setClass(mContent, MineCouponsActivity.class);
                        startActivity(intent);
                        break;
                    case 2://我的预存
                        intent.setClass(mContent, MineprestoreActivity.class);
                        startActivity(intent);
                        break;
                    case 3://我的消息
                        intent.setClass(mContent, MineMessageActivity.class);
                        startActivity(intent);
                        break;
                    case 4://我的关注
                        intent.setClass(mContent, CollectionBusinessesActivity.class);
                        startActivity(intent);
                        break;
                    case 5://收藏商品
                        intent.setClass(mContent, CollectionGoodsActivity.class);
                        startActivity(intent);
                        break;
                    case 6://我的地址
                        intent.setClass(mContent, MineAddressActivity.class);
                        startActivity(intent);
                        break;
                    case 7://我的设置
                        intent.setClass(mContent, MineSetActivity.class);
                        startActivity(intent);
                        break;
                    case 8://我的客服
                        callPhone("05926151179");
                        break;
                }
            }
        });


        View ll_login = view.findViewById(R.id.ll_login);
        ll_login.setOnClickListener(this);
        View ll_order1 = view.findViewById(R.id.ll_order1);
        ll_order1.setOnClickListener(this);
        View ll_order2 = view.findViewById(R.id.ll_order2);
        ll_order2.setOnClickListener(this);
        View ll_order3 = view.findViewById(R.id.ll_order3);
        ll_order3.setOnClickListener(this);
        ImageView iv_recommend = (ImageView)view.findViewById(R.id.iv_recommend);
        iv_recommend.setOnClickListener(this);

    }


    /**
     * 拨打电话（跳转到拨号界面，用户手动点击拨打）
     *
     * @param phoneNum 电话号码
     */
    public void callPhone(String phoneNum) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        Uri data = Uri.parse("tel:" + phoneNum);
        intent.setData(data);
        startActivity(intent);
    }



    class gvAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return icon.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = inflater.inflate(R.layout.gv_mine_item, null);
            ImageView iv_item = (ImageView) view.findViewById(R.id.iv_item);
            iv_item.setImageResource(icon[position]);
            TextView tv_item = (TextView) view.findViewById(R.id.tv_item);
            tv_item.setText(name[position]);
            return view;
        }
    }

    @Override
    public void onClick(View v) {

        // 如果未登录都进入登录界面
        if(!isLogin)
        {
            intent.setClass(mContent, LoginPhoneActivity.class);
            startActivity(intent);
            return;
        }

        switch (v.getId()) {
//            case R.id.ll_login:
//
////                    intent.setClass(mContent,UserInfoActivity.class);
////                    startActivity(intent);
//
//
//                intent.setClass(mContent, LoginPhoneActivity.class);
//                startActivity(intent);
//
//                break;
            case R.id.ll_order1:
                intent.putExtra("ordertype","全部订单");
                intent.setClass(mContent, OrderActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_order2:
                intent.putExtra("ordertype","待收货");
                intent.setClass(mContent, OrderActivity.class);
                startActivity(intent);
                break;
            case R.id.ll_order3:
                intent.putExtra("ordertype","消费与评价");
                intent.setClass(mContent, OrderActivity.class);
                startActivity(intent);
                break;
            case R.id.iv_recommend://推荐有理
                intent.setClass(mContent, RecommendActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();

//        sb.delete(0,sb.length());
        if (!MainActivity.mobile.equals("")) {

//            sb.append(MainActivity.mobile);
//            sb.replace(3, 7, "XXXX");
            tv_mobile.setText(MainActivity.userName);

            isLogin=true;
        } else {
            tv_mobile.setText("未登录");
            isLogin=false;
        }
    }
}
